<template>
  <div>
    <div class="select">
      <el-button type="primary" @click="addrole">添加角色</el-button>
    </div>
    <el-table class="list" :data="data.list" stripe style="width: 100%">
      <el-table-column prop="roleId" label="ID" width="60" />
      <el-table-column
        prop="roleName"
        :label="$t(`table.role_name`)"
        width="160"/>
      <el-table-column prop="role" :label="$t(`table.action`)" width="230">
        <template #default="scope">
          <el-button
            link
            @click="authoritychange(scope.row)"
            type="primary"
            size="small">修改权限</el-button>
        </template>
      </el-table-column>
      
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import { onMounted, reactive } from "@vue/runtime-core";
import { getRoleList } from "../../../http/api";
import { InitData, ListInt } from "../../../type/role";
import { useRouter } from 'vue-router'

const data = reactive(new InitData());
const router = useRouter()
onMounted(() => {
  getRoleList().then((res) => {
    data.list = res.data;
    // data.listData = res.data;
    // console.log(res.data);
  });
});
const authoritychange = (row:ListInt) => {
    console.log(row);
    router.push({
      path:'menuList',
      query:{
        id:row.roleId,
        authority:row.authority.join(',')
      }
    })
}
const addrole = () => {
    // data.isAdd = true
    ElMessageBox.prompt('请输入角色名', '添加', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  })
    .then(({ value }) => {
        if(value){
            data.list.unshift({
                roleName:value,
                roleId:data.list.length+1,
                authority:[]
            })
        }
      ElMessage({
        type: 'success',
        message:'新增成功',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消操作',
      })
    })
}
</script>

<style lang="scss" scoped>
.select {
    margin-bottom: 20px;
}
</style>